﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TinyCinema.Data;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using TinyCinema.Bussiness;

namespace TinyCinema.Bussiness.Cinema
{
    public class Room : DependencyObject
    {
        public static DependencyProperty lstRoomProperty = DependencyProperty.Register("lstRoom", typeof(ObservableCollection<Data.Room>), typeof(Room));
        public ObservableCollection<Data.Room> lstRoom
        {
            get { return (ObservableCollection<Data.Room>)GetValue(lstRoomProperty); }
            set
            {
                SetValue(lstRoomProperty, value);
            }
        }
        public static DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(Room));
        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set
            {
                SetValue(SelectedIndexProperty, value);
            }
        }
        public static ObservableCollection<Data.DeskType> lstDeskType
        {
            get { return Data.DeskType.lstType; }
        } 
        public Messenger Messenger { get; set; }
        public ICommand CreateCommand { get; set; }
        public ICommand DeleteCommand { get; set; }
        public ICommand SaveCommand { get; set; }
        public ICommand EnableDisableDeskCommand { get; set; }
        public Room()
        {
            Messenger = new Bussiness.Messenger();
            Data.Room.lstRoom = Data.Room.GetAllRoom();
            lstRoom = Data.Room.GetAllRoom();
           
            CreateCommand = new RelayCommand<object>(
                (s) => true,
                (s) =>
                {
                    if (lstDeskType.Count == 0) Messenger.Send(new Message.ListDeskTypeNotFound());
                    else lstRoom.Add(new Data.Room());
                });
            SaveCommand = new RelayCommand<Data.Room>(
                 (s) => true,
                 (s) =>
                 {

                     if (s != null)
                     {
                         try { s.Save(); }
                         catch { Data.Execution.Restore(); }
                     }
                 });
            DeleteCommand = new RelayCommand<object>(
                (s) => true,
                (s) =>
                {
                    var message1 = new Message.DeleteConfirmation();
                    Messenger.Send(message1);
                    if (message1.Result == MessageBoxResult.Yes)
                    {
                        try
                        {
                            lstRoom[SelectedIndex].Delete();
                            lstRoom.RemoveAt(SelectedIndex);
                        }
                        catch
                        {
                            var message2 = new Message.DeleteRoomFailed();
                            Messenger.Send(message2);
                            Data.Execution.Restore();
                        }

                    }
                }); 
            EnableDisableDeskCommand = new RelayCommand<Data.Desk>(
                 (s) => true,
                 (s) =>
                 {
                     s.Order = (s.Order==0) ? 1 : 0;
                     foreach(var Room in lstRoom)
                         if (Room.ID==s.Room.ID)
                         {
                             Room.DeskCountRefresh();
                             Room.DeskOrderRefresh();
                             return;
                         }
                 });
        }
    }
}
